﻿namespace LS.Nc6.Web.Entry.Areas.Cms.Controllers
{
    /// <summary>
    /// 内容标签
    /// </summary>
    [Route("[area]/[controller]/[action]")]
    [Area("Cms")]
    [ApiDescriptionSettings("Default")]
    public class ContentTagController : BaseAdminController
    {
        #region CmsContentTagController
        private readonly ICmsContentTagService _contentTagService;
        private readonly ICache _cache;
        public ContentTagController(ICmsContentTagService contentTagService, IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
            :base(cacheOptions, resolveNamed)
        {
            _contentTagService = contentTagService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }
        #endregion

        #region 页面
        public ActionResult Index()
        {
            SysPower(SysMenuConstant.MENU_CMS_SETTING_NRBQGL, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            return View();
        }
        public ActionResult Create()
        {
            SysPower(SysMenuConstant.MENU_CMS_SETTING_NRBQGL, SysMenuConstant.FUN_SELECT, UserInfo.ID.ToString());
            var model = new CmsContentTagEntity();
       
            return View("Edit", model);
        }
        public async Task<ActionResult> Edit(int id)
        {
            SysPower(SysMenuConstant.MENU_CMS_SETTING_NRBQGL, SysMenuConstant.FUN_UPDATE, UserInfo.ID.ToString());
            var model = await _contentTagService.GetByIdAsync(id);
            if (model == null)
            {
                return ErrorCustomMsg(ResponseHelper.NONEXISTENT);
            }
           
            return View(model);
        }
        #endregion

        #region  公共方法
        [HttpGet]
        public async Task<ResponseResultDataList> GetList()
        {
            string keyword = RequestHelper.FilterParam(Request.Query["title"]);
            var page = Request.Query["page"].ToString().GetQueryValueOrNull<int>(1);
            var limit = Request.Query["limit"].ToString().GetQueryValueOrNull<int>(20);
            string field = Request.Query["field"];
            string order = Request.Query["order"];

            var expable = Expressionable.Create<CmsContentTagEntity>();
            expable.AndIF(!string.IsNullOrEmpty(keyword), a => a.TagName.Contains(keyword));
            var exp = expable.ToExpression();
            var strField = RequestHelper.FilterParam(field);
            var strOrder = RequestHelper.FilterParam(order);

            Expression<Func<CmsContentTagEntity, object>> expressionOrder;
            if (string.IsNullOrEmpty(strField) || string.IsNullOrEmpty(strOrder))
            {
                expressionOrder = (a) => new { a.Id };
            }
            else
            {
                //实现字段排序
                expressionOrder = (a) => new { a.UseNum };
            }
            var result = await _contentTagService.PageList(exp, page, limit, expressionOrder);
            return new ResponseResultDataList { code = ResponseHelper.SEUCCESSCODE, message = "请求成功", data = result.Items, count = result.TotalCount };
        }



        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> SaveData([FromBody] ContentTagInput input)
        {
            bool power = true;
            var tags = RequestHelper.GetStringListByReturnAndNewline(input.TagNames);
            if (string.IsNullOrEmpty(input.TagNames) || tags.Count ==0)
            {
                return ResponseHelper.MessageResult(ResponseHelper.PARAMETER_CSCW);
            }
            long result = 0;
            power = SysPower(SysMenuConstant.MENU_CMS_SETTING_NRBQGL, SysMenuConstant.FUN_ADD, UserInfo.ID.ToString(), false);
            if (!power)
            {
                return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_QXZU);
            }
            var tagNames = await _contentTagService.GetTagNames();
            List<CmsContentTagEntity> list = new List<CmsContentTagEntity>();
            foreach (var item in tags)
            {
                if (RequestHelper.Contains(tagNames, item)) continue;
                list.Add(new CmsContentTagEntity { TagName=item, UseNum=0 });
            }
            
            result = await _contentTagService.InsertList(list);
            return ResponseHelper.SaveResult(result);
        }


        [HttpPost]
        public async  Task<ResponseResult> Delete([FromBody]DeletDataInput input)
        {
            var r = SysPower(SysMenuConstant.MENU_CMS_SETTING_NRBQGL, SysMenuConstant.FUN_DELETE, UserInfo.ID.ToString(), false);
            if (!r)
            {
                return ResponseHelper.MessageResult(ResponseHelper.PERMISSION_WSCQX);
            }
            if (string.IsNullOrEmpty(input.Ids))
            {
                return ResponseHelper.MessageResult(ResponseHelper.PARAMETER_CSCW);
            }
            int result =await _contentTagService.DeleteAsync(input.Ids.ToString());
            return ResponseHelper.DeleteResult(result);
        }

        #endregion


        #region 私有方法

        #endregion
    }
}